package com.bluedot.bank.dao;

import java.io.Serializable;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateDaoSupport<T, PK extends Serializable> implements
		Dao<T, PK> {
	private static SessionFactory sessionFactory;

	protected static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
		}
		return sessionFactory;
	}

	public static void closeSessionFactory() {
		if (sessionFactory != null) {
			sessionFactory.close();
		}
	}

	public static void initSession() {
		getSessionFactory().getCurrentSession().beginTransaction();
	}

	public Session getSession() {
		return getSessionFactory().getCurrentSession();
	}

	public static void rollback() {
		if (getSessionFactory().getCurrentSession() != null) {
			if (getSessionFactory().getCurrentSession().getTransaction() != null) {
				getSessionFactory().getCurrentSession().getTransaction()
						.rollback();
			}
		}
	}

	public static void commit() {
		if (getSessionFactory().getCurrentSession() != null) {
			if (getSessionFactory().getCurrentSession().getTransaction() != null) {
				getSessionFactory().getCurrentSession().getTransaction()
						.commit();
			}
		}
	}

	public static void close() {
		if (getSessionFactory().getCurrentSession() != null) {
			getSessionFactory().getCurrentSession().close();
		}
	}

	@SuppressWarnings("unchecked")
	protected Class entityClass;

	@Override
	public void create(T entity) {
		Session session = this.getSession();
		session.save(entity);
	}

	@Override
	public void delete(T entity) {
		Session session = this.getSession();
		session.delete(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T findById(PK id) {
		Session session = this.getSession();
		T entity = (T) session.get(this.entityClass, id);
		return entity;
	}

	@Override
	public void update(T entity) {
		Session session = this.getSession();
		session.update(entity);
	}

	@Override
	public void saveOrUpdate(T entity) {
		Session session = this.getSession();
		session.saveOrUpdate(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void browse(Pagination<T> pagination) {
		Session session = this.getSession();

		Query query = session.createQuery("select count(*) from "
				+ this.entityClass.getName());
		//Integer count = (Integer) query.uniqueResult();
		pagination.setRecordSum(((Long) query.uniqueResult()).intValue());

		if (pagination.getRecordSum() > 0) {
			String hql = "from " + this.entityClass.getName();

			if (pagination.getOrderFieldName() != null
					&& !pagination.getOrderFieldName().trim().isEmpty()) {
				hql += " oder by " + pagination.getOrderFieldName()
						+ (pagination.isDesc() ? "desc" : "");
			}

			query = session.createQuery(hql);
			query.setFirstResult(pagination.getSize()
					* (pagination.getNo() - 1));
			query.setMaxResults(pagination.getSize());

			pagination.setResults(query.list());
		}

	}

}
